package com.l024.android_ljl.view.main.activity;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.hjq.toast.ToastUtils;
import com.l024.android_ljl.R;
import com.l024.android_ljl.constant.PageConstant;
import com.l024.android_ljl.databinding.ActivitySubjectTypeBinding;
import com.l024.android_ljl.entity.SysTypeEntity;
import com.l024.android_ljl.viewmodel.main.SubjectViewModel;
import com.quantstack.llcommonlibrary.adapter.CommonAdapter;
import com.quantstack.llcommonlibrary.adapter.CommonViewHolder;
import com.quantstack.llcommonlibrary.base.BaseActivity;
import com.quantstack.llcommonlibrary.base.BaseViewModel;
import com.quantstack.llcommonlibrary.event.OnClickEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * 列表页
 */
public class SubjectTypeActivity extends BaseActivity<SubjectViewModel,ActivitySubjectTypeBinding> implements BaseViewModel.OnPageDataListener {
    private List<SysTypeEntity> list = new ArrayList<>();
    private CommonAdapter<SysTypeEntity> adapter;
    @Override
    public int getLayoutId() {
        return R.layout.activity_subject_type;
    }

    @Override
    public void afterCreate() {
        mViewModel.setOnPageDataListener(this);
        mViewModel.setBaseView(this);
        adapter = new CommonAdapter<SysTypeEntity>(list, new CommonAdapter.OnBindDataListener<SysTypeEntity>() {
            @Override
            public void onBindViewHolder(SysTypeEntity model, CommonViewHolder viewHolder, int type, int position) {
                viewHolder.setText(R.id.tv_type,model.getSubjectType());
                viewHolder.getView(R.id.ll_box).setOnClickListener(new OnClickEvent() {
                    @Override
                    public void singleClick(View v) {
                        //携带id跳转到试题
                        Bundle bundle = new Bundle();
                        bundle.putLong("typeId",model.getId());
                        go(SubjectTypeActivity.this,SubjectActivity.class,bundle);
                    }
                });
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.item_type;
            }
        });
        mViewDataBind.rvTypes.setLayoutManager(new LinearLayoutManager(this));
        mViewDataBind.rvTypes.setAdapter(adapter);
        mViewModel.getSubjectTypes();
    }

    @Override
    public void onSuccess(String tag) {
        switch (tag){
            case PageConstant.SUBJECTS_TYPES:{
                List<SysTypeEntity> value = mViewModel.getTypes().getValue();
                list.clear();
                list.addAll(value);
                adapter.notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    public void onError(String tag, String msg) {
        switch (tag){
            case PageConstant.SUBJECTS_TYPES:{
                ToastUtils.show(msg);
                break;
            }
        }
    }
}